<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：微折促销管理信息系统
// 代码日期：v3.0 2017-8-31
// 作者：杨道泉 
// 文件名：MPageController.class.php 
// 功能： 读取、创建手机页面

class PageController extends QnuiBaseController {
	public function index(){
		$this->display();
	}	
	public function dnd(){
		$this->display();
	}
	public function create_page(){
		$shop_model_id = I("shop_model_id");
		
		$CMP = D("ShopCompView");
		$wh["sid"]=session("SHOP_ID");
		$wh["shop_model_id"]=$shop_model_id;
		$comps = $CMP->where($wh)->getField("id,component_id,shop_model_id,name,model_name,comp_ordinal,preview,comp_code,model_preview,model_private,cat,max_count,mix");
		if($comps === null){
			$err = C("ErrCode");
			$res = new \stdClass();
			$res->code = 911;
			$res->msg = $err[911];
			$this->ajaxReturn(pre_resp($res));
			die();
		}
		$comp = current($comps);
		$PG = M("PageInshop");
		$page["sid"]=session("SHOP_ID");
		$page["name"]=$comp["model_name"];
		$page["model_id"]=$comp["shop_model_id"];
		$page["preview"]=$comp["model_preview"];
		$page["creat_date"]=date("Y-m-d H:i:s");
		$page["status"]="normal";
		$page_id = $PG->add($page);
		
		
		$CMPS = M("PageComponentsInpage");
		foreach($comps as $c){
			$comp_data = Array();
			$comp_data["sid"] = session("SHOP_ID");
			$comp_data["name"] = $c["name"];
			$comp_data["preview"] = $c["preview"];
			$comp_data["ordinal"] = $c["comp_ordinal"];
			$comp_data["code"] = $c["comp_code"];
			$comp_data["model_private"] = $c["model_private"];
			$comp_data["status"] = "show";
			$comp_data["page_id"] = $page_id;
			$comp_data["max_count"] = $c["max_count"];
			$comp_data["cat"] = $c["cat"];
			$comp_data["mix"] = $c["mix"];
			$component_inpage_id = $CMPS->add($comp_data);
			$CMPS->execute("INSERT INTO __PAGE_COMPONENT_SETTINGS_INPAGE__ (sid,page_id,component_inpage_id,field_name,name,val)
						    SELECT '".session("SHOP_ID")."','".$page_id."','".$component_inpage_id."',field_name,name,val FROM __PAGE_COMPONENT_SETTINGS__ 
							WHERE component_id= '".$c["component_id"]."'");
			
			
		}
		
		Vendor('taobaosdk/top/request/MaQrcodeCommonCreateRequest');
		$req = new \MaQrcodeCommonCreateRequest;
		$req->setType("url");
		//$req->setContent(C("MOB_URL")."/index.php/QnuiRingsMobile?_wx_tpl=".C("MOB_URL")."/index.php/QnuiRingsMobile/Index/get_page_js?page_id=".$page_id);//http://localhost/index.php/QnuiRingsMobile?_wx_tpl=http://localhost/index.php/QnuiRingsMobile/Index/get_page_js?page_id=14
		$req->setContent(C("MOB_URL")."/index.php/QnuiRingsMobile/Index/get_page?page_id=".$page_id."&wxUseWKWebView=YES");
		$req->setName("无线活动页面");
		$req->setChannelName("微折无线页面");
		$req->setStyle("EF4F2B");
		$req->setSize("175");
		$req->setNeedEps("true");
		$req->setLogo("http://img01.taobaocdn.com/imgextra/T1Od8YFT8eXXXXXXXX");
		$resp = $this->c->execute($req,session("TOP_SESSION"));
		
		$PG->where(Array("id"=>$page_id))
		   ->save(Array(
					"channel_id"=>$resp->modules->qrcode_d_o->channel_id->__toString(),
					"channel_name"=>$resp->modules->qrcode_d_o->channel_name->__toString(),
					"qrcode_url"=>$resp->modules->qrcode_d_o->qrcode_url->__toString(),
					"short_url"=>$resp->modules->qrcode_d_o->short_url->__toString(),
					"url"=>$resp->modules->qrcode_d_o->url->__toString()
				 ));
				 
		$res = new \stdClass();
		$res->page_id = $page_id;
		$this->ajaxReturn(pre_resp($res));
	}
	public function get_pages(){
		$page = I("page");
		$page_size = I("page_size");
		$is_delete = I("is_delete");
		$PG = M("PageInshop");
		if($is_delete=="true"){
			$wh = "sid='".session("SHOP_ID")."' AND (status='delete')";
		}else{
			$wh = "sid='".session("SHOP_ID")."' AND (status='normal' OR status='pubing' OR status='pubed' OR status='offline' OR status='error')";
		}
		$total = $PG->where($wh)->count();
		$pages = $PG->where($wh)
					->limit(($page-1)*$page_size,$page_size)
					->getField("id,name,preview,qrcode_url,url,short_url,status");
		if($pages === null){
			$pages = Array();
		}
		$res = new \stdClass();
		$res->pages = array_values($pages);
		$res->total = (int)$total;
		$this->ajaxReturn(pre_resp($res));
	}
	public function get_page_components(){
		$page_id=I("page_id");
		$COMP = D("PageComponents");
		$COMP->page_id = $page_id;
		$COMP->sid = session("SHOP_ID");
		$components = $COMP->getComponets();
		$res = new \stdClass();
		$res->components = $components;
		$this->ajaxReturn(pre_resp($res));
	}
	public function remove_page(){
		$page_id = I("page_id");
		$PG = M("PageInshop");
		$wh["id"]=$page_id;
		$wh["sid"]=session("SHOP_ID");
		$status = $PG->where($wh)->getField("status");
		
		$err = C("ErrCode");
		$res = new \stdClass();
		$res->page_id = $page_id;
		if($status=="pubing"){
			$res->code = 914;
			$res->msg = $err[914];
			$this->ajaxReturn(pre_resp($res));
			die();
		}
		if($status=="pubed"){
			$res->code = 915;
			$res->msg = $err[915];
			$this->ajaxReturn(pre_resp($res));
			die();
		}
		$re = $PG->where($wh)->save(Array("status"=>"delete"));
		
		$res = new \stdClass();
		$res->page_id = $page_id;
		if($re === false){
			$res->code = 912;
		}
		$this->ajaxReturn(pre_resp($res));
	}
	public function publish_page(){
		$page_id = I("page_id");
		$err_msg = C("ErrCode");
		
		$COMP = D("PageComponents");
		$COMP->page_id = $page_id;
		$COMP->sid = session("SHOP_ID");
		$settings = $COMP->getComponetsSettings();
		$has_prom_set = false;
		foreach($settings as $set){
			if(strpos($set["code"],"countdown_prom")>=0){
				foreach($set as $k=>$s){
					if($k=="countdownId"){
						$has_prom_set=true;
					}
				}
			}
		}
		$PG = M("PageInshop");
		$wh["id"]=$page_id;
		$wh["sid"]=session("SHOP_ID");
		if($has_prom_set){
			$re = $PG->where($wh)->save(Array("status"=>"pubed","js"=>json_encode($settings)));
		}else{
			$PG->where($wh)->save(Array("status"=>"error"));
			$re = false;
			$MSG = M("PagePubingMessage");
			$msg_data["sid"]=session("SHOP_ID");
			$msg_data["page_id"]=$page_id;
			$msg_data["code"]="error_no_prom_id";
			$msg_data["msg"]="您没有选择倒计时折扣！";
			$msg_data["date"]=date("Y-m-d H:i:s");
			$MSG->add($msg_data);
		}
		
		
		$res = new \stdClass();
		$res->page_id = $page_id;
		$res->settings = $settings;
		if($re===false){
			$res->code = 912;
			$res->msg=$msg_data["msg"];
		}else{
			$res->code = 200;
		}
		/*
		//依赖后端的处理过程
		$wh["id"]=$page_id;
		$wh["sid"]=session("SHOP_ID");
		$PGV = D("PageView");
		$tag_action = $PGV->where($wh)->getField("tag_action");

		$res = new \stdClass();
		$res->page_id = $page_id;
		
		try {
			$con = new \Stomp(C("ActiveMQ"),C("ActiveMQUser"),C("ActiveMQPass"));
			$msg_data=Array("pageId"=>$page_id);
            $re = $con->send($tag_action, json_encode(Array("page_id"=>$page_id,"sid"=>session("SHOP_ID"))),array("amq-msg-type" => "text"));
			if($re){
				$PG = M("PageInshop");
				$wh["id"]=$page_id;
				$wh["sid"]=session("SHOP_ID");
				$re = $PG->where($wh)->save(Array("status"=>"pubing"));
				if($re===false){
					$res->code = 912;
				}else{
					$res->code = 200;
				}
			}else{
				$res->code = 916;
			}
		} catch (\StompException $e) {
			$re=false;
			$res->code = 916;
			$res->msg="";
		}
		
		$res->msg=$err_msg[$res->code];
		*/
		$res->page_id = $page_id;
		$this->ajaxReturn(pre_resp($res));
	}
	public function un_publish_page(){
		$page_id = I("page_id");
		
		$PG = M("PageInshop");
		$wh["id"]=$page_id;
		$wh["sid"]=session("SHOP_ID");
		
		$re = $PG->where($wh)->save(Array("status"=>"offline","js"=>""));
		
		$res = new \stdClass();
		$res->page_id = $page_id;
		if($re === false){
			$res->code = 912;
		}
		$this->ajaxReturn(pre_resp($res));
	}
	public function get_page_pub_error(){
		$page_id = I("page_id");
		$MSG = M("PagePubingMessage");
		$msg = $MSG->where(Array("sid"=>session("SHOP_ID"),"page_id"=>$page_id))->getField("id,code,msg,date");
		
		$res = new \stdClass();
		$res->msg = array_values($msg);
		if($re === false){
			$res->code = 912;
		}
		$this->ajaxReturn(pre_resp($res));
	}
	public function edit_page_title(){
		$page_id = I("page_id");
		$name = I("name");
		$preview = I("preview");
		
		$PG = M("PageInshop");
		$wh["id"]=$page_id;
		$wh["sid"]=session("SHOP_ID");
		
		$re = $PG->where($wh)->save(Array("name"=>$name,"preview"=>$preview));
		
		$res = new \stdClass();
		$res->page_id = $page_id;
		if($re === false){
			$res->code = 912;
		}
		$this->ajaxReturn(pre_resp($res));
	}
}